module write_results

  use globals
  
  implicit none
  integer :: temp_file=17
  integer :: doma_file=19
  integer :: e_file=21
  
contains
!--------------------------------------------------------------------------
subroutine file_ini

  implicit none
  
  open(temp_file,FILE=trim(adjustl(opath))//trim(adjustl(temp_out)),STATUS='UNKNOWN')
  open(doma_file,FILE=trim(adjustl(opath))//trim(adjustl(dom_out)),STATUS='UNKNOWN')
  open(e_file,FILE=trim(adjustl(opath))//trim(adjustl(ep_out)),STATUS='UNKNOWN')
	  
  write(temp_file,'(1x,a,11x,a,17x,a,15x,a,14x,a,15x,a,12x,a,7x,a)')'tstep','time','Te', &
		& 'Tgold','Tw1','Tw2', 'E(*10^-10)', 'Ne'  !,'Heatloss,','Heatin'
		
end subroutine file_ini

!--------------------------------------------------------------------------
subroutine write_T(step,force)

  use space
  implicit none
  integer :: ii, step, inrep, force
  
  if (time .ge. tmax) then
	 do ii=1,NI
		write(doma_file,*) (r(ii)), Tw(ii)
	 enddo
  endif
  
!  if (mod(step,500) .eq. 0) then
!  	 do ii=1,floor(NI/4.0d0)
!		write(doma_file,*) (r(ii)), Tw(ii)
!	 enddo
!  endif

  inrep = nrep + 1

  if (mod(step,10) .eq. 0) then
     !write(*,*) step
     !write(*,*) time,' ',step,' ',Te,Ta,Tw(1),Tw(2),heatLoss, heatIn         
     write(temp_file,'(i9,2x,d17.11,2x,f17.11,2x,f16.11,2x,f16.11,2x,f16.11,2x,d17.11,2x,i7)') &
	   step,time,Te,Ta,Tw(1),Tw(2),Efield*10.0**(-10.0),Etot!,heatLoss, heatIn
     write(e_file,*) time,' ', Et, E_total
 
  elseif (force .eq. 1) then
     write(temp_file,'(i9,2x,d17.11,2x,f17.11,2x,f16.11,2x,f16.11,2x,f16.11,2x,d17.11,2x,i7)') &
	   step,time,Te,Ta,Tw(1),Tw(2),Efield*10.0**(-10.0),Etot
     write(e_file,*) time,' ', Et, E_total

  elseif (Te.lt.Te_last .and. Te_flag.eq.0) then
     write(temp_file,'(i9,2x,d17.11,2x,f17.11,2x,f16.11,2x,f16.11,2x,f16.11,2x,d17.11,2x,i7)') &
	   step-1,time-dt,Te_last,Ta_last,Tw_last,Tw(2),Efield*10.0**(-10.0), Etot
     write(e_file,*) time,' ', Et, E_total
     Te_flag = 1

  elseif (Ta.lt.Ta_last .and. Ta_flag.eq.0) then
     write(temp_file,'(i9,2x,d17.11,2x,f17.11,2x,f16.11,2x,f16.11,2x,f16.11,2x,d17.11,2x,i7)') &
	   step-1,time-dt,Te_last,Ta_last,Tw_last,Tw(2),Efield*10.0**(-10.0), Etot
     write(e_file,*) time,' ', Et, E_total
     Ta_flag = 1

  elseif (Tw(1).lt.Tw_last .and. Tw_flag.eq.0) then
     write(temp_file,'(i9,2x,d17.11,2x,f17.11,2x,f16.11,2x,f16.11,2x,f16.11,2x,d17.11,2x,i7)') &
	   step-1,time-dt,Te_last,Ta_last,Tw_last,Tw(2),Efield*10.0**(-10.0), Etot
     write(e_file,*) time,' ', Et, E_total
     Tw_flag = 1
  endif
  
  if (MOD(step,5000).eq.0) then 
     write(*,'(a,i9,a,E9.3,a,I3)') "step: ",step, "   time: ", time, "   pulse no: ", inrep
  endif

end subroutine write_T 
!--------------------------------------------------------------------------
subroutine close_files

  implicit none
  
  close(doma_file)
  close(temp_file)
  close(e_file)
  
  write (*,*) " Program completed with no errors."
  write (*,*) " Final time: ", time
  write (*,*) " "
  
end subroutine close_files
!--------------------------------------------------------------------------

end module write_results
